//*****************************************************************************
//

#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "interrupt.h"
#include "sysctl.h"
#include "timer.h"
#include "osram96x16x1.h"
#include "gpio.h"
#include "adc.h"
#include "uart.h"

//*****************************************************************************	//792
const unsigned char lab_base[180] = {


	0xFF, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01, 0x01, 0x01, 
	0x1F, 0x01, 0x01, 0x01, 0xF1, 0x01, 0x01, 0x01, 0xF1, 0x01, 0x01, 0x01, 
	0xF1, 0x01, 0x01, 0x01, 0xF1, 0x11, 0x11, 0x11, 0x01, 0x01, 0x01, 0xFF, 
	0xFF, 0x01, 0x01, 0x01, 0xF1, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 

	0x11, 0x11, 0x11, 0x11, 0xF1, 0x10, 0x10, 0x10, 0xFF, 0x00, 0x00, 0x00, 
	0xFF, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0xF1, 0x01, 0x01, 0x01, 0xFF, 
	0xFF, 0x08, 0x08, 0x08, 0x08, 0x08, 0x88, 0x08, 0x0F, 0x01, 0x41, 0x41, 
	0x71, 0x11, 0x11, 0x11, 0xF1, 0x80, 0x80, 0x80, 0x8F, 0x80, 0x80, 0x80, 

	0x83, 0x82, 0x82, 0xFE, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 
	0xFF, 0x80, 0x80, 0x80, 0x88, 0x88, 0x8F, 0x8C, 0x8C, 0x8C, 0x8C, 0x8C, 
	0x8C, 0x8C, 0x8C, 0x8C, 0x8F, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 
	0x88, 0x88, 0x88, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0xFF


};
//*****************************************************************************
const unsigned char point[2] = {
	0x03, 0x03
};							   

//*****************************************************************************
unsigned char buffer[180] = {0};

//*****************************************************************************
//Global variables:

unsigned int pos_buf = 0; //coordinate, from which buffer starts
unsigned long val = 512; //this variable keeps the current value of the ADC
unsigned long tmp_val;

//*****************************************************************************
//function prototypes: 

void _UpdateBuf(unsigned int);
void UpdateBuffer(unsigned int, short);

//*****************************************************************************

void
DisplayPoint() {
	OSRAMImageDraw(point, 48, 1, 2, 1);
}
//*****************************************************************************
//function that updates the content of the buffer in direction, determined by the ADC 
void
UpdateBuffer(unsigned int posX, short direction) {
//direction: 1 - left, 2 - right, 3 - up, 4 - down
	if(direction == 1) {
//		_UpdateBuf(pos_buf_X - 1, pos_buf_Y);
//		--pos_buf;
	}
	if(direction == 2) {
//		_UpdateBuf(pos_buf_X + 1, pos_buf_Y);
//		++pos_buf;
	}
	if(direction == 3) {
//		_UpdateBuf(pos_buf_X , pos_buf_Y + 1);
//		++pos_buf_Y;
	}
	if(direction == 4) {
//		_UpdateBuf(pos_buf_X , pos_buf_Y - 1);
//		--pos_buf_Y;
	}	
}
//*****************************************************************************
//Help function for UpdateBuffer()
//function that copies database from X and Y in the buffer

//*****************************************************************************
//
void Display() {
	//display the labyrinth
//	OSRAMImageDraw(buffer, 0, 0, 192, 2);
	//display the point
	//OSRAMImageDraw(lab_base, 48, 0, 36, 2);
	DisplayPoint();
}

//*****************************************************************************


#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif


//*****************************************************************************

#define PUSH_BUTTON GPIO_PIN_4
#define USER_LED GPIO_PIN_5

//*****************************************************************************
//simple delay function
void
delay(int a) {
	while(--a);
}
//
//*****************************************************************************
//
//*****************************************************************************
int
main(void)
{
//unsigned long val_tmp;
//	unsigned long curr = 512; //the middle value of ADC
//	unsigned short is;
	//
    // Set the clocking to run directly from the crystal.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);
 
	IntMasterDisable();
    //
    // Initialize the OLED display and write status.
    //

    OSRAM96x16x1Init(true);
	//
    // Display the updated image on the display.
    //
	//OSRAMImageDraw(image, 0, 0, 60, 1);


    //
    // Enable the peripherals used by this example.
    //
//  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//  SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
	
	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);
	//Enable the UART
	SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Set GPIO A0 and A1 as UART pins.
    //
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

	
	//configure the LED
	GPIOPinTypeGPIOInput(GPIO_PORTC_BASE , PUSH_BUTTON );
    GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, USER_LED);

    //
    // Configure the ADC to sample the potentiometer when the timer expires.
    //
    ADCSequenceConfigure(ADC_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
    ADCSequenceStepConfigure(ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
    ADCSequenceEnable(ADC_BASE, 0);

//	ADCIntEnable(ADC_BASE, 0);
	
//	_UpdateBuf(0);
	Display();
	

	//
    // Loop forever while the timers run.
    //
	for(;;) {

		ADCProcessorTrigger(ADC_BASE, 0);
		
		while(!ADCIntStatus(ADC_BASE, 0, false)) {
		}
		ADCSequenceDataGet(ADC_BASE, 0, &tmp_val);
		val = (20 * val)/100	+ (80 * tmp_val)/100;
        val = ((tmp_val*96)/1023);
		val = tmp_val;

			OSRAMClear();
			OSRAMImageDraw(lab_base, val, 0, 36, 2);

//		val_tmp = val;
/*		
		if (curr > val)
			if (flag) {
				UpdateBuffer(pos_buf_X, pos_buf_Y, 4);
			}
			else {
				UpdateBuffer(pos_buf_X, pos_buf_Y, 1);				
			}
		else if (curr < val) {
			if (flag) {
				UpdateBuffer(, pos_buf_X, pos_buf_Y, 3);
			}
			else {
				UpdateBuffer(pos_buf_X, pos_buf_Y, 2);				
			}
		}
*/	
		
	/*	IntMasterEnable();
		is = GPIOPinRead( GPIO_PORTC_BASE , PUSH_BUTTON );	
		if(is == 0 )
		    if(flag)
				flag = 0;
			else
				flag = 1;	
	 */
	}

}
